package com.gene.controller.item;

/**
 * Created by lenovo on 2018/1/15.
 */

import com.gene.pojo.TbItem;
import com.gene.pojo.entity.Product;
import com.gene.service.ITbItemService;
import com.gene.utils.CodeValue;
import com.gene.utils.MsgValue;
import com.gene.utils.PageResult;
import com.gene.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品
 */
@Controller
@RequestMapping("/gene-safe/item")
public class ItemController {
    @Autowired
    private ITbItemService service;


    @RequestMapping(value="/selectId",method = RequestMethod.POST)
    @ResponseBody
    public ResultData selectId(TbItem tbItem){
        ResultData result=new ResultData();
        Product product = service.selectId(tbItem);
        if(null != product){
            result.setStatus("1");
            result.setData(product);
            result.setMsg("查询成功");
        }else{
            result.setStatus("0");
            result.setMsg("查询失败");
        }
        return result;
    }

    /**
     *  商品的查询，带多条件查询,一次传参数只能选择其中的一种
     *  参数为tbItem
     *  1: 参数  id（商品id） 查询单个数据
     *  2：参数  hot=1   查询热门的数据  有排序
     *  3：参数  cid   按照cid（商品类型）来查询
     *
     *  注意 所有数据 status（商品状态）=1（正常）
     */
    @RequestMapping(value="/itemList",method = RequestMethod.POST)
    @ResponseBody
    public ResultData itemList(TbItem tbItem){
        ResultData result=new ResultData();
        List<TbItem> list = service.tbItemLsit(tbItem);
        if(list.size()>0){
            result.setStatus("1");
            result.setData(list);
            result.setMsg("查询成功");
        }else{
            result.setStatus("0");
            result.setMsg("查询失败");
        }
        return result;
    }


    /**
     * 添加商品
     */
    @RequestMapping(value="/add",method = RequestMethod.POST)
    @ResponseBody
    public ResultData add(TbItem item,String desc){
        ResultData result=new ResultData();
        if(null==item){
            result.setErrorCode(CodeValue.USER_PARAMETERS_ISNULL);
            result.setMsg(MsgValue.USER_PARAMETERS_ISNULL);
            return result;
        }
        TbItem tbItem=service.add(item,desc);
        result.setData(tbItem);
        result.setMsg(MsgValue.SUCCESS);
        result.setErrorCode(CodeValue.SUCCESS);
        return result;
    }
    /**
     * 查询单个商品
     */
    @RequestMapping(value="/query/{id}")
    @ResponseBody
    public ResultData query(@PathVariable("id")Long id){
        ResultData result=new ResultData();
        TbItem tbItem= service.queryid(id);
        result.setData(tbItem);
        result.setMsg(MsgValue.SUCCESS);
        result.setErrorCode(CodeValue.SUCCESS);
        return result;
    }
    /**
     * 分页查询类目商品
     */
    @RequestMapping(value="/list",method = RequestMethod.POST)
    @ResponseBody
    public ResultData querylist(@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "10")Integer pageSize,Long cid){
        ResultData result=new ResultData();
        PageResult<TbItem> list=service.queryCidList(pageNum, pageSize, cid);
        return result;
    }
    /**
     * 修改商品
     */
    @RequestMapping(value="/edit",method = RequestMethod.POST)
    @ResponseBody
    public ResultData edit(TbItem item,String desc){
        ResultData result=new ResultData();
        service.edit(item,desc);
        return result;
    }
    /**
     * 删除商品
     */
    @RequestMapping(value="/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResultData delete(Long id){
        ResultData result=new ResultData();
        service.delete(id);
        return result;
    }


    /**
     *  商品的查询，按照title(产品名称)模糊搜索
     *
     *  注意 所有数据 status（商品状态）=1（正常）
     */
    @RequestMapping(value="/title",method = RequestMethod.POST)
    @ResponseBody
    public ResultData title(TbItem tbItem){
        ResultData result=new ResultData();
        List<TbItem> list = service.title(tbItem);
        if(list.size()>0){
            result.setStatus("1");
            result.setData(list);
            result.setMsg("查询成功");
        }else{
            result.setStatus("0");
            result.setMsg("查询失败");
        }
        return result;
    }



}
